package com.lightboat.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.lightboat.common.core.domain.entity.SysUser;
import com.lightboat.common.utils.SecurityUtils;
import com.lightboat.system.domain.SysNotice;
import com.lightboat.system.mapper.SysNoticeMapper;
import com.lightboat.system.mapper.SysUserMapper;
import com.lightboat.system.service.ISysNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 公告 服务层实现
 *
 * @author lightboat
 */
@Service
public class SysNoticeServiceImpl extends ServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {
    @Autowired
    private SysNoticeMapper noticeMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.selectById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        return noticeMapper.insert(notice);
    }

    /**
     * 异步新增公告
     * @param notice
     */
    @Override
    public void insertAsyncNotice(SysNotice notice) {
        if (notice.getUserId() == null && ObjectUtil.isEmpty(notice.getRoleCode())) {
            return;
        }
        if (ObjectUtil.isNotEmpty(notice.getUserId())) {
            SysUser sysUser = userMapper.selectUserById(notice.getUserId());
            if (sysUser == null) {
                return;
            }
            notice.setDeptId(sysUser.getDeptId());
            notice.setStatus("0");
            notice.setIsRead(0);
            noticeMapper.insert(notice);
        }
        if (ObjectUtil.isNotEmpty(notice.getRoleCode())) {
            SysUser sysUser1 = new SysUser();
            sysUser1.setRoleKeys(Lists.newArrayList(notice.getRoleCode()));
            List<SysUser> sysUsers = userMapper.selectUserByRole(sysUser1);
            if (ObjectUtil.isEmpty(sysUsers)) {
                return;
            }
            List<SysNotice> notices = sysUsers.parallelStream().map(m -> {
                SysNotice sysNotice = BeanUtil.copyProperties(notice, SysNotice.class);
                sysNotice.setDeptId(m.getDeptId());
                sysNotice.setStatus("0");
                sysNotice.setIsRead(0);
                return sysNotice;
            }).toList();
            noticeMapper.insert(notices);
        }

    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        return noticeMapper.updateById(notice);
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        return noticeMapper.deleteNoticeById(noticeId);
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        return noticeMapper.deleteNoticeByIds(noticeIds);
    }

    @Override
    public Map<String, Object> countUser(String type) {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setMsgType(type);
        sysNotice.setStatus("0");
        sysNotice.setUserId(SecurityUtils.getUserId());
        return baseMapper.countUser(sysNotice);
    }

    /**
     * 用户未读消息类型统计
     * @param type
     * @return
     */
    @Override
    public Map<String, Object> countUnReadMsgType(String type) {
        SysNotice sysNotice = new SysNotice();
        sysNotice.setMsgType(type);
        sysNotice.setStatus("0");
        sysNotice.setUserId(SecurityUtils.getUserId());
        return baseMapper.countUnReadMsgType(sysNotice);
    }
}
